package com.example.com.skills_utf8;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;

import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.graphics.Paint;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;

import com.digirun.android.base.BaseActivity;
import com.utils.AppUtils;
import com.utils.User;
import com.utils.UserService;
import com.utils.Utils1;

public class ShopsCommentActivity extends BaseActivity{
 
	ShopsCommentActivity activity = this;
	
	ListView comments_listview;
	String pageNo ="0";
	String pageSize ="10";
	String shopId="";
	

	
	public LinkedHashMap<String, Object> netdata = new LinkedHashMap<String, Object>();
	
	
	
	
	ShopsCommentAdapter adapter_shops_comment;
	
	ArrayList<ShopsCommentBean> CommentArray = new ArrayList<ShopsCommentBean>();
	
	
	

	
	
	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		super.onBackPressed();
		
		finish();
		overridePendingTransition(R.anim.in_from_left, R.anim.out_to_right);
		
	}
	
	@Override
	protected int initPageLayoutID() {
		// TODO Auto-generated method stub
		return R.layout.activity_shops_comment;
	}

	@Override
	protected void initPageView() {
		// TODO Auto-generated method stub
		
		
		System.out.println("class: "+ ShopsCommentActivity.class);
		
		
	
		Paint p = new Paint();
		
	
		
		
		
		comments_listview = (ListView)findViewById(R.id.comments_listview);
		
		
		
	}

	@Override
	protected void initPageViewListener() {
		// TODO Auto-generated method stub
		
	
	
				
		
	}

	@Override
	protected void initProcess(Bundle arg0) {
		// TODO Auto-generated method stub

//		Intent intent =getIntent();
//	 shopId=	intent.getStringExtra("shopId");
	

	 
	 adapter_shops_comment = new ShopsCommentAdapter(CommentArray, activity);
	 comments_listview.setAdapter(adapter_shops_comment);
	 

	 User user = new User();
	 user.setUserId("624");
	 UserService.setUser(user);
	 
	 	
//		netdata.put("shopId", shopId);
//		netdata.put("pageNo", pageNo);
//		netdata.put("pageSize", pageSize);
//		requestNetData(new ShopsCommentNetHelper(ShopsCommentActivity.this, "get"));
	}
	
protected void onResume() {
	super.onResume();
	
	if(pageNo.equals("0")){
	netdata.put("shopId", shopId);
	netdata.put("pageNo", pageNo);
	netdata.put("pageSize", pageSize);
	
	
	
	new com.utils.NetHelper( this ){

		@Override
		public String init(ArrayList<BasicNameValuePair> map) {
			// TODO Auto-generated method stub
			
//			map.add(new BasicNameValuePair("type", "0"));
			map.add( new BasicNameValuePair("pageNo", "0"));
			map.add( new BasicNameValuePair("pageSize", "30"));
			map.add( new BasicNameValuePair("shopId", "1"));
//			
//			return "http://115.29.191.0/wtt/api.php/users/order/";
			
			return "http://115.29.191.0/wtt/api.php/shops/comment/";
		}

		@Override
		public void onComplete(String message) throws JSONException {
			// TODO Auto-generated method stub
			
			initData(message);
			
		}
	
		
	}.start_raw_GET();
	
	
//	requestNetData(new ShopsCommentNetHelper(ShopsCommentActivity.this, "get"));
	}
};
	
	
	
	
	
	public void initData(String message){
		
		
		
	//	CommentArray.clear();

		try {
			JSONObject obj = new JSONObject(message.toString());

			if (obj.getString("result").equals("0")) {

				JSONObject data = obj.getJSONObject("data");

				
				
				if( data.isNull("commentList") ){
					int pn = Integer.valueOf(pageNo);
					if(pn==0)
						Utils1.showToastShort(activity, "暂时还没有评论信息~");
					else
						Utils1.showToastShort(activity, "没有更多评价信息了~");
					return;
				}
					
				
				
				JSONArray jsonArray = data.getJSONArray("commentList");

				for (int i = 0; i < jsonArray.length(); i++) {
				
					JSONObject jsonObject2 = (JSONObject)jsonArray.opt(i);
				
					ShopsCommentBean bean = new ShopsCommentBean();
					  
					String commentId = jsonObject2.getString("commentId"); // 
					String fid =jsonObject2.getString("fid"); // 评论者id
					String userName =  jsonObject2.getString("userName");   //
					String nickName = jsonObject2.getString("nickName");
					String time =jsonObject2.getString("time");  //""
					String content =jsonObject2.getString("content"); //
					String point =jsonObject2.getString("point"); //
					
					
				//	if(time.indexOf("-") != -1)
				//	{
					SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
					;
					
					time +="000";
					long time_long =Long.parseLong(time);
					bean.content =content;
					bean.time = sdf.format(new Date(time_long));
					bean.user_name =userName;
					bean.nick_name = nickName;
					bean.rating_bar =Float.parseFloat(point);
					bean.fid = fid;
					bean.user_photo = jsonObject2.getString("userImg");
					bean.flag = jsonObject2.getString("flag");		
					
					CommentArray.add(bean);
					
				}
				

				Utils1.NoticeNew(activity, jsonArray.length());
				

				// 提醒更新UI
		//		MyHandler.sendEmptyMessage(0); 
				
				// 刷新listview
				adapter_shops_comment.notifyDataSetChanged();



			} // end if

		} catch (JSONException e) {
			// TODO Auto-generated catch block
			Utils1.json_erro(activity, e);

		}

	}
	
	
	public void netResponse_foucs(String message, View view) {
		// TODO Auto-generated method stub

		try {
			JSONObject obj = new JSONObject(message.toString());

			Toast.makeText(activity, obj.getString("message"), Toast.LENGTH_SHORT).show();
			
		 
			if (obj.getString("result").equals("0")) {

				CommentArray.get(adapter_shops_comment.mClickPos).flag ="1";
				adapter_shops_comment.notifyDataSetChanged();
				
			}
			
				
		}catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println("JSONException" + e.toString());

		}
		
	
	}
	

}


class ShopsCommentBean
{
	String content, time, user_name, user_photo;
	String nick_name;
	float rating_bar;
	
	 String fid ; // 评论者id
	 
	 String flag; //是否已经关注
};	




class ShopsCommentAdapter extends BaseAdapter {
	ShopsCommentAdapter adapter =this;
	ShopsCommentActivity activity;
	
	ArrayList<ShopsCommentBean> data;
	private LayoutInflater inflater;

	public int mClickPos ;
	
	
	public ShopsCommentAdapter(ArrayList<ShopsCommentBean> array, ShopsCommentActivity activity) {
		// TODO Auto-generated constructor stub

		data = array;
		inflater = activity.getLayoutInflater();

		this.activity = activity;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return data.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return data.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub

		if(true){
	//	if (convertView == null) {
			holder = new ViewHolder();
			convertView = inflater.inflate(R.layout.adapter_shops_comment,
					null);
			holder.user_name = (TextView) convertView.findViewById(R.id.user_name);
			holder.time = (TextView) convertView.findViewById(R.id.time);
			holder.content = (TextView) convertView.findViewById(R.id.content);
			
			holder.rating_bar =(RatingBar)convertView.findViewById(R.id.rating_bar);
			
			holder.user_photo =(ImageView)convertView.findViewById(R.id.pic);
			
			
			holder.btn_focus =(TextView)convertView.findViewById(R.id.btn_focus);
			holder.btn_focus.setOnClickListener( new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					
					
					Utils1.showToastShort(activity, ""+position);
					
	 				
					// 这里要用view,findchildat�?....
					if( UserService.isLogin() )
					{
						
						mClickPos = position;
						
						String fid =data.get(position).fid;
						if(UserService.getUser().getUserId().equals(fid) == false){
							activity.netdata.put("fid", fid);
						
							if( data.get(position).flag.equals("0")) //还没关注
							{
								/*
								new com.dmapp.utils.NetHelper(){

									@Override
									public String init(
											ArrayList<BasicNameValuePair> map) {
										// TODO Auto-generated method stub
										
										map.add(new BasicNameValuePair("userId", UserService.getUser().getUserId()));
										map.add(new BasicNameValuePair("fid", (String)activity.netdata.get("fid") ));
										
										return AppUtils.HOST+"api.php/users/cancel_follow/";
									}

									@Override
									public void onComplete(String message)
											throws JSONException {
										// TODO Auto-generated method stub
										
								//		activity.netResponse_foucs(message);
										
									}
									
								}.start_POST();
								*/
							}
							else{
							
								
								///*
								// 取消关注
								new com.utils.NetHelper( activity ){

									@Override
									public String init(
											ArrayList<BasicNameValuePair> map) {
										// TODO Auto-generated method stub
										
										map.add(new BasicNameValuePair("userId", UserService.getUser().getUserId()));
										map.add(new BasicNameValuePair("fid", (String)activity.netdata.get("fid") ));
										
										return AppUtils.HOST+"api.php/users/cancel_follow/";
									}

									@Override
									public void onComplete(String message)
											throws JSONException {
										// TODO Auto-generated method stub
										
										JSONObject obj = new JSONObject(message.toString());
										Toast.makeText(activity, obj.getString("message"), Toast.LENGTH_SHORT).show();
										
										
										data.get(position).flag ="0"; // 取消关注状�?
										adapter.notifyDataSetChanged();
										
									}
									
								}.start_raw_POST();
								//*/
								
								
							}
								
						}
						else{
						
							Utils1.showToastShort(activity, "不能关注自己~");
						}
							
					}
					else
					{
						
					}
					
					
				}
			});
			
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

//		holder.user_name.setText(data.get(position).user_name);
		holder.user_name.setText(data.get(position).nick_name);
		holder.time.setText(data.get(position).time);
		holder.content.setText(data.get(position).content);
		holder.rating_bar.setRating(data.get(position).rating_bar);

		
		
		if(data.get(position).flag.equals("1")){  // 已经关注
			
			// 显示取消关注按钮
			holder.btn_focus.setBackgroundResource(R.drawable.products_te_dian); 
			holder.btn_focus.setText("-取消关注");
			holder.btn_focus.setTextColor(0xff777777);
		                
		}
		else if( data.get(position).flag.equals("0") ){ // 取消关注
			
			// 显示加关注按�?
			holder.btn_focus.setBackgroundResource(R.drawable.green_button);
			holder.btn_focus.setText("+加关注");
			holder.btn_focus.setTextColor(0xffffffff);
			
		}
		
		
	
		activity.inflateImage(data.get(position).user_photo,
				holder.user_photo,
				R.drawable.userpic, 50);
		
		
	//	AppUtils.mDownloader.download(data.get(position).user_photo, holder.user_photo);
		
		
		
		
		
		return convertView;

	}

	ViewHolder holder;

	private class ViewHolder {
		TextView content,time, user_name;
		RatingBar rating_bar;
		
		TextView btn_focus;
		
		ImageView user_photo;
	}

};




/*
{
    "result": 0,
    "message": "成功",
    "data": {
        "commentList": [
            {
                "commentId": "34",
                "fid": "14",
                "userName": "13092157886",
                "time": "1385625002",
                "content": "Hhh\n",
                "point": "5"
            },
            {
                "commentId": "33",
                "fid": "9",
                "userName": "15850666905",
                "time": "1385624426",
                "content": "s",
                "point": "2"
            },
            {
                "commentId": "23",
                "fid": "1",
                "userName": "joshua",
                "time": "1385561276",
                "content": "手撒发生发卡上了飞机啊上课了飞机啊水力发电看",
                "point": "4"
            },
            {
                "commentId": "19",
                "fid": "1",
                "userName": "joshua",
                "time": "1385560698",
                "content": "不好�?,
                "point": "1"
            },
            {
                "commentId": "18",
                "fid": "1",
                "userName": "joshua",
                "time": "1385560679",
                "content": "我有意见,这家很差�?,
                "point": "2"
            },
            {
                "commentId": "13",
                "fid": "5",
                "userName": "13852298451",
                "time": "1385531014",
                "content": "刚刚更换",
                "point": "3"
            }
        ],
        "shopId": "3"
    }
}
*/